package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.service.ProjectCostService;
import com.workManager.service.ProjectTotalCostService;
import com.workManager.util.WebUtil;
import com.workManager.vo.CurrentUserVO;
import com.workManager.vo.ProjectLaborCostVO;
import com.workManager.vo.ProjectTotalCostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by strangevy on 2017/6/28
 */
@Controller
@RequestMapping("/projectTotalCost")
public class ProjectTotalCostController extends BaseController {

    @Autowired
    private ProjectTotalCostService projectTotalCostService;


    @ApiOperation(value = "项目总成本Excel文件上传接口", notes = "项目总成本Excel文件上传接口")
    @RequestMapping(value = "/upLoadExcel", method = RequestMethod.POST)
    public Object upLoadExcel(@RequestParam() MultipartFile file) {
        if (file==null){
            return null;
        }
        ModelMap modelMap = new ModelMap();
        String name = file.getOriginalFilename();
        long size = file.getSize();

        if (name == null || "".equals(name) && size == 0) {
            return null;
        }

        String msg = "上传成功";
        try {
            projectTotalCostService.uploadExcel(name, file);
        } catch (Exception e) {
            return setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR, e.getMessage());
        }
        return setModelMap(modelMap, RespCode.OK, msg);
    }

    @ApiOperation(value ="项目总成本查询",notes = "项目总成本查询")
    @RequestMapping(value="/countLaberCost",method = RequestMethod.POST)
    /*@RequiresPermissions(value = "/projectCost/countLaberCost")*/
    public Object countLaberCost(HttpServletRequest request,
                                 @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                 @RequestParam(value="pageSize", required = false) String pageSize,
                                 @RequestParam(value = "salMonth", required =  false) String salMonth,
                                 @RequestParam(value = "projectCode", required = false) String projectCode,
                                 @RequestParam(value = "projectName", required = false) String projectName,
                                 @RequestParam(value = "loginCode", required = false) String loginCode
                ){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("salMonth",salMonth);
        map.put("loginCode",loginCode);

        PageInfo<ProjectTotalCostVo> projectTotalCostVoPageInfo =  projectTotalCostService.countLaberCost(map);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",projectTotalCostVoPageInfo.getTotal());
        dataMap.put("rows",projectTotalCostVoPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);

    }


    @ApiOperation(value ="按项目编号查询项目总成本",notes = "按项目编号查询项目总成本")
    @RequestMapping(value="/selectByProjectCode",method = RequestMethod.POST)
    /*@RequiresPermissions(value = "/projectCost/countLaberCost")*/
    public Object selectByProjectCode(@RequestParam(value = "projectCode", required = true) String projectCode){
        ModelMap modelMap = new ModelMap();
        return  setSuccessModelMap(modelMap,projectTotalCostService.selectByProjectCode(projectCode));

    }
}
